package com.yoomiito.app.ui.fragment.cicle

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseLazyFragmentPresenter
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.MaterialContent
import com.yoomiito.app.model.MaterialLabel
import com.yoomiito.app.net.YmApi

class NewMaterialPresenter(appConfig: AppConfig) : BaseLazyFragmentPresenter<NewMaterialFragment>(appConfig){

    fun getLabel(){
        YmApi.getYmService()
                .materialLabel
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<List<MaterialLabel>>(handleException()){
                    override fun onSuccess(t: List<MaterialLabel>?) {
                        v.showLabel(t as MutableList<MaterialLabel>?)
                    }

                    override fun onFail(error: ApiException?) {
                        v.dismissLoading()
                    }

                })
    }

    fun getList(id:Long,page:Int){
        YmApi.getYmService()
                .getMaterialList(id.toString(),page)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<BaseList<MaterialContent>>(handleException()){

                    override fun onSuccess(t: BaseList<MaterialContent>?) {
                        v.showList(t)
                    }

                    override fun onFail(error: ApiException?) {
                        v.dismissLoading()
                    }

                })
    }
}